package com.cl.esjsoup.jsoup;

import com.cl.esjsoup.pojo.Comment;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class JsoupUtils {
    public static void main(String[] args) throws IOException {
//        String urlStr = "https://www.agefans.vip";
//        List<Comment> comments = getComments(urlStr);
//        System.out.println(comments);

//        String urlStr = "https://www.agefans.vip/detail/20210199";
//        List<Comment> comments = getCommentList(urlStr);
//        System.out.println(comments);

        String urlStr = "https://www.agefans.vip/catalog/all-all-all-all-all-time-1";
        ArrayList<Comment> comments = getCommentArrayList(urlStr);
        System.out.println(comments);
    }

    /**
     * @param urlStr https://www.agefans.vip/catalog/all-all-all-all-all-time-{num}
     * @return ArrayList<Comment>
     * @throws IOException IOException
     */
    public static ArrayList<Comment> getCommentArrayList(String urlStr) throws IOException {
        URL url = new URL(urlStr);
        String protocol = url.getProtocol();
        String host = url.getHost();
        Document parse = Jsoup.parse(url, 30000);
        Elements blockcontent1 = parse.getElementsByClass("blockcontent1");
        Elements elementList = blockcontent1.get(0).getElementsByClass("cell");
        ArrayList<Comment> comments = new ArrayList<>();
        for (Element element : elementList) {
            Element cellImformName = element.getElementsByClass("cell_imform_name").get(0);
            String href = cellImformName.attr("href");
            href = protocol + "://" + host + href;
            String name = cellImformName.text();

            String src = element.getElementsByTag("img").attr("src");
            if (src.startsWith("//")) {
                src = "https:" + src;
            }
            Comment comment = new Comment(href, name, src);
            comments.add(comment);
        }
        return comments;
    }

    private static List<Comment> getCommentList(String urlStr) throws IOException {
        URL url = new URL(urlStr);
        Document parse = Jsoup.parse(url, 30000);
        Element main0 = parse.getElementById("main0");
        assert main0 != null;
        Elements li = main0.getElementsByTag("li");
        List<Comment> comments = new ArrayList<>();
        for (Element next : li) {
            Elements aTag = next.getElementsByTag("a");
            String href = aTag.attr("href");
            String a = aTag.text();
            Comment comment = new Comment(href, a);
            comments.add(comment);
        }
        return comments;
    }

    private static List<Comment> getComments(String urlStr) throws IOException {
        URL url = new URL(urlStr);
        Document parse = Jsoup.parse(url, 30000);
        Element animeUpdate = parse.getElementById("anime_update");
        assert animeUpdate != null;
        Elements li = animeUpdate.getElementsByTag("li");
        List<Comment> comments = new ArrayList<>();
        for (Element next : li) {
            Elements aTag = next.getElementsByTag("a");
            String href = aTag.attr("href");
            String a = aTag.text();
            String span = next.getElementsByTag("span").text();
            Comment comment = new Comment(href, a, span);
            comments.add(comment);
        }
        return comments;
    }

}
